<template>
  <BaseForm v-model="formDataModel" @close="$emit('callBack')" />

</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import ConsoleUserApi from '/@/api/console/ConsoleUserApi.ts';
import { translate } from "/@/i18n";
import FORM_FIELD_TYPE from "/@/constants/FormFieldType.ts";
import { md5Encode } from "/@/utils/encrypt.ts";
defineOptions({
  name: 'ConsoleUserEdit',
})
const props = defineProps({
  modelValue: {

  }
})
const emit = defineEmits(['fetch-data', 'callBack'])

const formRef = ref<FormInstance>();
const title = ref<string>('');
const dialogFormVisible = ref<boolean>(false);

const consoleUserId = props.modelValue.query.consoleUserId

const formDataModel = ref({
  columnCount: 1,
  expandLimit: 6,
  autoFit: false,
  columnWidth: '300px',
  labelPosition: 'left',
  labelWidth: '100px',
  modelData: {},  //表单参数值 初始化
  callback: async (any: object) => {  //表单执行成功后回调
    if (any.password != any.confirmPassword) {
      $baseMessage('两次密码输入不一致', 'error', 'vab-hey-message-success');
      return
    }
    //完成数据提交
    const { message, success } = await ConsoleUserApi.resetPassword({
      companyAccountId: consoleUserId,
      password: md5Encode(any.password)
    })
    $baseMessage(message, 'success', 'vab-hey-message-success');


    //事件及数据推送到上一层父类
    if (success) {
      emit('callBack', any); //把表单数据推送到上一层
    }
  },
  items: [  //表单item数据初始化

    {
      label: "新密码",
      field: "password",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "确认密码",
      field: "confirmPassword",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    }
  ],
})






//页面数据加载
onMounted(async () => {

  //初始化数据model
  if (props.modelValue.query.consoleUserId) {
    // const {data} = await ConsoleUserApi.getDetail({
    //         consoleUserId: props.modelValue.query.consoleUserId   //从路由中获取参数
    // })
    // formDataModel.value.modelData = data;

    //todo 更新密码
  }

})


//方法暴露区域  todo
defineExpose({

})

</script>
